﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sexybeauty.Framework.Common;
using System.Linq.Expressions;

namespace Sexybeauty.Core.Inf.Entities
{
    public abstract class BaseEntity<T>
    {
        public virtual int PostModify()
        {
            try
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Trace,
                    String.Format("对象：{0}执行PostModify操作。对象值为：{1}",
                    this.GetType().FullName, this.ToJson()));
            }
            catch { }

            using (Context ctx = new Context())
            {
                ctx.Entry(this).State = System.Data.EntityState.Modified;
                return ctx.SaveChanges();
            }
        }
        public virtual int PostDelete()
        {
            try
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Trace,
                    String.Format("对象：{0}执行PostDelete操作。对象值为：{1}",
                    this.GetType().FullName, this.ToJson()));
            }
            catch { }
            using (Context ctx = new Context())
            {
                ctx.Entry(this).State = System.Data.EntityState.Deleted;
                return ctx.SaveChanges();
            }
        }
        public virtual int PostAdd()
        {
            try
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Trace,
                    String.Format("对象：{0}执行PostAdd操作。对象值为：{1}",
                    this.GetType().FullName, this.ToJson()));
            }
            catch { }
            using (Context ctx = new Context())
            {
                ctx.Entry(this).State = System.Data.EntityState.Added;
                return ctx.SaveChanges();
            }
        }
        public abstract T Get(Expression<Func<T, bool>> where);
    }
}
